package BVRClient;

import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;

import BVRClient.BVRC_Player.PLAYER_TEAM;
import BVRNetworking.BVRN_Definitions;
import BVRNetworking.BVRN_Packet;

public class BVRClientHandler extends IoHandlerAdapter
{
	private BVRC_GamePlayState gameplayState = null;
	
	BVRClientHandler(BVRC_GamePlayState gameplayState)
	{
		this.gameplayState = gameplayState;
	}
	
	public void exceptionCaught(IoSession session, Throwable t) throws Exception
	{
		t.printStackTrace();
	}
	
	public void sessionClosed(IoSession session)
	{
		BVRC_Settings.session = null;
	}
	
	public void sessionOpened(IoSession session)
	{
		BVRC_Settings.session = session;
		
		BVRC_PacketGenerator.connectHeader().send(session);
	}
	
	public void messageReceived(IoSession session, Object msg) throws Exception
	{
		String message = msg.toString();
		
		if (message.length() <= 0)
			return;
		
		BVRN_Packet packet = new BVRN_Packet(message);
		
		switch (packet.getType())
		{
			case BVRN_Definitions.BVRN_CREATELOCALPLAYER:
			{
				if (this.gameplayState.getPlayerFromID(packet.getParameterInt(0)) != null)
					break;
				
				this.gameplayState.localPlayer = new BVRC_Player();
				
				this.gameplayState.localPlayer.setPlayerID(packet.getParameterInt(0));
				this.gameplayState.localPlayer.setPosition(new BVRC_Point(packet.getParameterFloat(1), packet.getParameterFloat(2)));
				this.gameplayState.localPlayer.setTeam(PLAYER_TEAM.valueOf(packet.getParameter(4)));
				this.gameplayState.localPlayer.setSpriteFromTeam();
				this.gameplayState.localPlayer.setRotation(packet.getParameterFloat(3));
				
				this.gameplayState.players.add(this.gameplayState.localPlayer);
			}
			break;
			
			case BVRN_Definitions.BVRN_CREATE_PLAYER:
			{
				if (this.gameplayState.getPlayerFromID((int) packet.getParameterFloat(0)) != null)
					break;
				
				BVRC_Player player = new BVRC_Player();
				
				player.setPlayerID(packet.getParameterInt(0));
				player.setPosition(new BVRC_Point(packet.getParameterFloat(1), packet.getParameterFloat(2)));
				player.setTeam(PLAYER_TEAM.valueOf(packet.getParameter(4)));
				player.setSpriteFromTeam();
				player.setRotation(packet.getParameterFloat(3));
				
				this.gameplayState.players.add(player);
			}
			break;
			
			case BVRN_Definitions.BVRN_PLAYER_ROTATION:
			{
				BVRC_Player player = this.gameplayState.getPlayerFromID(packet.getParameterInt(0));
				
				if (player != null)
					player.setRotation(packet.getParameterFloat(1));
			}
			break;
			
			case BVRN_Definitions.BVRN_SET_PLAYER_VELOCITY:
			{
				BVRC_Player player = this.gameplayState.getPlayerFromID(packet.getParameterInt(0));
				
				if (player == null)
					break;
				
				player.setMovementSpeedX(packet.getParameterInt(1));
				player.setMovementSpeedY(packet.getParameterInt(2));
			}
			break;
			
			case BVRN_Definitions.BVRN_SET_PLAYER_POSITION:
			{
				BVRC_Player player = this.gameplayState.getPlayerFromID(packet.getParameterInt(0));
				
				if (player == null)
					break;
				
				player.setPosition(new BVRC_Point(packet.getParameterFloat(1), packet.getParameterFloat(2)));
			}
			break;
			
			case BVRN_Definitions.BVRN_SETMOVEMENTSPEED:
			{
				if (packet.getParameterInt(0) > 0)
					BVRC_Settings.movementSpeed = packet.getParameterInt(0);
			}
			break;
			
			case BVRN_Definitions.BVRN_CREATE_PROJECTILE:
			{
				BVRC_Player player = new BVRC_Player();
				
				player.setTeam(PLAYER_TEAM.valueOf(packet.getParameter(3)));
				
				BVRC_Projectile projectile = new BVRC_Projectile(BVRC_Settings.projectileSpeed, player);
				projectile.setPosition((int) packet.getParameterFloat(0), (int) packet.getParameterFloat(1));
				projectile.setRotation(packet.getParameterFloat(2));
				projectile.setOwnerId(packet.getParameterInt(4));
				
				this.gameplayState.projectiles.add(projectile);
			}
			break;
		}
	}
}